﻿using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;

namespace FlightService
{
    /// <summary>
    /// Static parser to create XML response or read XML request
    /// </summary>
    public static class XmlParser
    {
        /// <summary>
        /// Transforms an XML request to a list of Flight objects
        /// </summary>
        /// <param name="xml">special XML request</param>
        /// <returns>List of Flight objects</returns>
        public static List<Flight> XmlToFlights(string xml)
        {
            List<Flight> flights = new List<Flight>();

            try
            {
                StringReader stringReader = new StringReader(xml);
                XmlTextReader reader = new XmlTextReader(stringReader);
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "Flights")
                    {
                        while (reader.Read() && (reader.NodeType != XmlNodeType.EndElement || reader.Name != "Flights"))
                        {
                            if (reader.NodeType == XmlNodeType.Element && reader.Name == "Flight")
                            {
                                Flight flight = new Flight();
                                flights.Add(flight);
                                while (reader.MoveToNextAttribute())
                                    if (reader.Name == "from")
                                        flight.CityFrom = reader.Value;
                                    else if (reader.Name == "destination")
                                        flight.CityTo = reader.Value;
                                    else if (reader.Name == "date")
                                        flight.DateFrom = DateTime.ParseExact(reader.Value, "dd-MM-yyyy",
                                                System.Globalization.CultureInfo.InvariantCulture);

                                //while (reader.Read() && (reader.NodeType != XmlNodeType.EndElement || reader.Name != "Flight"))
                                //{
                                //    //while (reader.MoveToNextAttribute())
                                //    //    if (reader.Name == "from")
                                //    //        flight.CityFrom = reader.Value;
                                //    //    else if (reader.Name == "destination")
                                //    //        flight.CityTo = reader.Value;
                                //    //    else if (reader.Name == "date")
                                //    //        flight.DateFrom = DateTime.ParseExact(reader.Value, "dd-MM-yyyy",
                                //    //                System.Globalization.CultureInfo.InvariantCulture);
                                //}
                            }
                        }
                    }
                }
                reader.Close();
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid XML file");
            }

            return flights;
        }

        /// <summary>
        /// Transforms a list of Flight objects to an XML response
        /// </summary>
        /// <param name="flights">List of Flight objects</param>
        /// <returns>XML response</returns>
        public static string FlightsToXml(List<Flight> flights)
        {
            string xml = string.Empty;
            if (flights.Count == 0)
                return "No flights found";

            xml = "<Flights>";
            foreach (Flight f in flights)
            {
                //f.DateFrom = new DateTime(f.DateFrom.Year, f.DateFrom.Month, f.DateFrom.Day,
                //    int.Parse(String.Concat(f.TimeFrom[0], f.TimeFrom[1])), 
                //    int.Parse(String.Concat(f.TimeFrom[3], f.TimeFrom[4])),
                //    int.Parse(String.Concat(f.TimeFrom[6], f.TimeFrom[7])));
                xml += "<Flight dateFrom=\"" + f.DateFrom + "\" " +
                       "dateTo=\"" + f.DateTo + "\" " +
                       "cityFrom=\"" + f.CityFrom + "\" " +
                       "cityTo=\"" + f.CityTo + "\" " +
                    //"timeFrom=\"" + f.TimeFrom + "\" " +
                    //"timeTo=\"" + f.TimeTo + "\" " +
                       "airline=\"" + f.Airline + "\" " +
                       "price=\"" + (f.Price * f.Adults + f.PriceChildren * f.Children)  + "\"/>";
            }
            xml += "</Flights>";
            return xml;
        }

        /// <summary>
        /// Retrieves special request options from a given XML and puts them in a Request object
        /// </summary>
        /// <param name="xml">XML request</param>
        /// <returns>Request object</returns>
        public static Request GetRequestOptionsFromXml(string xml)
        {
            Request request = new Request();

            try
            {
                StringReader stringReader = new StringReader(xml);
                XmlTextReader reader = new XmlTextReader(stringReader);
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "Flights")
                    {
                        while (reader.MoveToNextAttribute())
                        {
                            switch (reader.Name)
                            {
                                case "return":
                                    request.IsReturn = reader.Value == "true";
                                    break;
                                case "option":
                                    request.Option = reader.Value;
                                    break;
                                case "adults":
                                    request.Adults = int.Parse(reader.Value);
                                    break;
                                case "children":
                                    request.Children = int.Parse(reader.Value);
                                    break;
                            }
                        }
                    }
                    break;
                }
                reader.Close();
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid XML file");
            }

            return request;
        }
    }
}